<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>3 给点击事件添加防抖</title>
</head>

<body>
  <button id="btn">btn</button>

  <script>
    let btn = document.querySelector("#btn");

    function debounce(fn, wait = 300, immediate = false) {
      let timer = null;
      return function anonymouse(...args) {
        let now = immediate && !timer;
        timer && clearTimeout(timer);
        timer = setTimeout(function () {
          timer = null;
          !immediate ? fn.call(this, ...args) : null;
        }, wait)
        now ? fn.call(this, ...args) : null;
      }
    }

    function fn(ev, a, b) {
      // console.log(ev)
      console.log('fn...', a, b)
    }
    // btn.addEventListener("click", debounce(fn, 300, false));
    btn.addEventListener("click", debounce(function (ev) {
      fn(ev, 10, 2)
    }, 300, false));
  </script>
</body>

</html>